package com.rpay.web.controller.user;

import com.github.pagehelper.PageInfo;
import com.rpay.common.dto.cost.CostDTO;
import com.rpay.common.dto.user.ConversionDTO;
import com.rpay.common.dto.user.UserDTO;
import com.rpay.common.dto.user.UserTradeDTO;
import com.rpay.common.dto.user.WithdrawDTO;
import com.rpay.common.enums.SmsCodeEnum;
import com.rpay.common.enums.SmsTypeEnum;
import com.rpay.common.vo.PcsResult;
import com.rpay.service.auth.IAuthService;
import com.rpay.service.config.ServiceThreadLocal;
import com.rpay.service.sms.ISmsCodeService;
import com.rpay.service.user.IUserTradeService;
import com.rpay.service.user.impl.UserTradeServiceImpl;
import com.rpay.web.controller.BaseController;
import com.rpay.web.interceptor.authentic.AccessControl;
import com.rpay.web.interceptor.authentic.AccessControlType;
import com.rpay.web.param.Convert;
import com.rpay.web.param.Transfer;
import com.rpay.web.param.Withdraw;
import org.apache.ibatis.session.RowBounds;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "api/trade")
@AccessControl(type = {AccessControlType.SECRET, AccessControlType.ACCESS_TOKEN})
public class UserTradeController extends BaseController {

    @Autowired
    private IUserTradeService iUserTradeService;
    @Autowired
    private UserTradeServiceImpl userTradeService;

    @Autowired
    private IAuthService iAuthService;
    @Autowired
    private ISmsCodeService iSmsCodeService;

    @GetMapping(value = "")
    public PcsResult trade(@RequestParam(name = "page", defaultValue = "1") Integer page,
                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                           @RequestParam(name = "type", required = false) String type,
                           @RequestParam(name = "status", required = false) String status,
                           @RequestParam(name = "startDate", required = false) String startDate,
                           @RequestParam(name = "endDate", required = false) String endDate) {
        int userId = ServiceThreadLocal.getUserId();
        Map param = new HashMap();
        param.put("type", type);
        param.put("status", status);
        param.put("startDate", startDate);
        param.put("endDate", endDate);
        param.put("userId", userId);

//        PageInfo<Map> resultUserTradeList = iUserTradeService.getSelfTrades(param, rowBounds);
        Map resultMap = userTradeService.getUserTradeDataList(param, new RowBounds((page - 1) * pageSize, pageSize));

        return PcsResult.build(resultMap);
    }

    // 提现申请
    @PostMapping(value = "/withdraw")
    public PcsResult withdraw(@Validated @RequestBody Withdraw withdraw) {
        // 验证码等信息
        UserDTO user = getUser();
        if (SmsTypeEnum.MAIL.getCode().equals(withdraw.getType())) {
            iSmsCodeService.verifyMail(SmsCodeEnum.WITHDRAW.getCode(), user.getEmail(), withdraw.getCode());
        } else {
            iAuthService.verifyAuth(user.getSecretKey(), Integer.valueOf(withdraw.getCode()));
        }

        UserTradeDTO userTradeDTO = new UserTradeDTO();
        userTradeDTO.setUserId(getUserId());
        userTradeDTO.setType("WITHDRAW");
        userTradeDTO.setStatus("VERIFY");
        userTradeDTO.setRippleAmount(new BigDecimal(withdraw.getAmount()));
        userTradeDTO.setDestination_wallet_id(withdraw.getWallet_id());
        userTradeDTO.setRate(new BigDecimal(withdraw.getRate()));
        iUserTradeService.withdraw(userTradeDTO, withdraw.getRate());
        return PcsResult.build(userTradeDTO);
    }

    // 转换申请
    @PostMapping(value = "/conversion")
    public PcsResult conversion(@Validated @RequestBody Convert convert) {
        // 验证码等信息
        UserDTO user = getUser();
        if (SmsTypeEnum.MAIL.getCode().equals(convert.getType())) {
            iSmsCodeService.verifyMail(SmsCodeEnum.CONVERT.getCode(), user.getEmail(), convert.getCode());
        } else {
            iAuthService.verifyAuth(user.getSecretKey(), Integer.valueOf(convert.getCode()));
        }

        // insert user trade
        UserTradeDTO userTradeDTO = new UserTradeDTO();
        userTradeDTO.setUserId(getUserId());
        userTradeDTO.setType("CONVERT");
        userTradeDTO.setStatus("END");
        userTradeDTO.setRpayAmount(new BigDecimal(convert.getAmount()));
        iUserTradeService.conversion(userTradeDTO);
        return PcsResult.build(userTradeDTO);
    }

    @GetMapping(value = "/rate")
    public PcsResult getRate() {
        BigDecimal rate = iUserTradeService.getRate();
        return PcsResult.build(rate);
    }

    @GetMapping(value = "/cost")
    public PcsResult getCosts() {
        CostDTO cost1 = new CostDTO();
        cost1.setId(1);
        cost1.setName("T+1提现");
        cost1.setEnName("Withdraw can be done in  one day through T+1");
        cost1.setDesc("T+1提现");
        cost1.setValue("0.1");


        CostDTO cost2 = new CostDTO();
        cost2.setId(2);
        cost2.setName("T+3提现");
        cost1.setEnName("Withdraw can be done in  3 days through T+3");
        cost2.setDesc("3天后到账");
        cost2.setValue("0.05");


        CostDTO cost3 = new CostDTO();
        cost3.setId(3);
        cost3.setName("T+7提现");
        cost3.setEnName("Withdraw can be done in  7 days through T+7");
        cost3.setDesc("7天后到账");
        cost3.setValue("0.025");

        CostDTO[] costs = new CostDTO[3];
        costs[0] = cost1;
        costs[1] = cost2;
        costs[2] = cost3;

        return PcsResult.build(costs);
    }

    @PostMapping(value = "/transfer")
    public PcsResult transfer(@Validated @RequestBody Transfer transfer) {
        // 验证码等信息
        UserDTO user = getUser();
        if (SmsTypeEnum.MAIL.getCode().equals(transfer.getType())) {
            iSmsCodeService.verifyMail(SmsCodeEnum.TRANSFER.getCode(), user.getEmail(), transfer.getCode());
        } else {
            iAuthService.verifyAuth(user.getSecretKey(), Integer.valueOf(transfer.getCode()));
        }
        int userId = getUserId();
        iUserTradeService.transfer(userId, transfer.getDestUserId(), transfer.getAmount());
        return new PcsResult();
    }

}
